<template>
  <StudentLayout>
    <el-select
      v-model="sdept_id"
      placeholder="Select"
      size="large"
      style="width: 240px"
      @change="sdeptChange"
    >
      <el-option
        v-for="item in allData.sdept"
        :key="item._id"
        :label="item.name"
        :value="item._id"
      />
    </el-select>
    <el-button style="margin-left: 15px" type="primary" :icon="Plus" @click="addSdeptDig()">新增班级</el-button>
    <el-table :data="changeClassData" style="width: 100%">
      <el-table-column prop="name" label="班级名称" />
      <el-table-column prop="sdept_name" label="院系" />
      <el-table-column prop="description" label="班级描述" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="addSdeptDig(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="确认删除吗?"
            @confirm="delClassSub(scope.row)"
          >
            <template #reference>
              <el-button
                size="small"
                type="danger"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </StudentLayout>
  <el-dialog v-model="dialogFormVisible" :title="isEdit?'编辑班级':'新增班级'"
    width="500">
    <el-form :model="form" @submit.native.prevent label-width="auto">
      <el-form-item label="院系">
        <el-select v-model="form.sdept_id" placeholder="请选择院系">
          <el-option :label="item.name" :value="item._id"
            v-for="(item, index) in allData.sdept"
            :key="index"
            />
        </el-select>
      </el-form-item>
      <el-form-item label="班级名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="班级描述">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关闭</el-button>
        <el-button type="primary" @click="addClassSub()">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import StudentLayout from '@/layouts/StudentLayout.vue'
import  { getClassList, getSdeptList, addClass, editClass, delClass } from '@/composables/school'

const dialogFormVisible = ref(false)
const isEdit = ref(false)
const editID = ref()
const sdept_id = ref()
let form = reactive({
  sdept_id: '',
  name: '',
  description: ''
})
const sdeptChange = () => {
  changeClass()
}
const delClassSub = async (row) => {
  const params = {
    _id: row._id
  }
  await delClass(params).then(res => {
      changeClass()
      ElMessage({
        message: '班级删除成功',
        type: 'success',
      })
  })
}

const addSdeptDig = async (index, row) => {
  dialogFormVisible.value = true
  form.sdept_id = '' 
  form.name = '' 
  form.description = ''
  isEdit.value = false
  if (row) {
    isEdit.value = true
    form.name = row.name 
    form.sdept_id = row.sdept_id 
    form.description = row.description
    editID.value = row._id
  }
}
const addClassSub = async () => {
  if (isEdit.value){
    form._id = editID.value
    await editClass(form).then(res => {
      dialogFormVisible.value = false
      changeClass()
      ElMessage({
        message: '班级编辑成功',
        type: 'success',
      })
    })
  } else {
    await addClass(form).then(res => {
      dialogFormVisible.value = false
      changeClass()
      ElMessage({
        message: '班级添加成功',
        type: 'success',
      })
    })
  }
}
const listAjax = () => {
  const params = {
    sdept_id: sdept_id.value
  }
  return getClassList(params).then(res => {
    return res.data
  })
}
const changeClass = async () => {
  allData.value.classs = await listAjax()
}

const {data: allData} = await useAsyncData('sdept', async () => {
  const [sdept, classs] = await Promise.all([
    getSdeptList().then(res => {
      return res.data
    }),
    listAjax()
  ])
  return { sdept, classs }
})
const changeClassData = computed(() => {
  if(allData.value.classs){
    return allData.value.classs.map(el => {
      allData.value.sdept.forEach(fe => {
        if (el.sdept_id === fe._id) {
          el.sdept_name = fe.name
        }
      })
      return el
    })
  }
})
</script>
<style scoped>

</style>